home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / GUSI-mods / GUSISIOUX.cp < prev   
Encoding:
Text File  |  1996-10-17  |  4.9 KB  |  240 lines  |  [TEXT/CWIE]

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand unified socket interface
  3. File        :    GUSISIOUX.cp    -    Interface to Metrowerks SIOUX library
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log$
  8. *********************************************************************/
  9.  
  10. #include <GUSIFile_P.h>
  11. #include <ioctl.h>
  12. #include <console.h>
  13.  
  14. #include <Events.h>
  15. #include <LowMem.h>
  16.  
  17. /************************ SIOUXSocket members ************************/
  18.  
  19. /* This declaration lies about the return type */
  20. extern "C" void SIOUXHandleOneEvent(EventRecord *userevent);
  21.  
  22. GUSIEvtHandler    GUSISIOUXEvents[]    =    {
  23.     SIOUXHandleOneEvent,        // nullEvent
  24.     
  25.     SIOUXHandleOneEvent,        // mouseDown
  26.     SIOUXHandleOneEvent,        // mouseUp
  27.     nil,                            // keyDown
  28.     nil,
  29.     
  30.     nil,                            // autoKey
  31.     SIOUXHandleOneEvent,        // updateEvt
  32.     SIOUXHandleOneEvent,        // diskEvt
  33.     SIOUXHandleOneEvent,        // activateEvt
  34.     
  35.     nil,
  36.     nil,
  37.     nil,
  38.     nil,
  39.     
  40.     nil,
  41.     nil,
  42.     SIOUXHandleOneEvent,        // osEvt
  43.     nil,
  44.     
  45.     nil,
  46.     nil,
  47.     nil,
  48.     nil,
  49.     
  50.     nil,
  51.     nil,
  52.     nil,
  53. };
  54.  
  55. /************************ Declaration of SIOUXSocket ************************/
  56.  
  57. class SIOUXSocket : public Socket    {        
  58.     friend class SIOUXSocketDomain;    
  59.     
  60.                     SIOUXSocket();
  61.                     
  62.     virtual         ~SIOUXSocket();
  63. protected:
  64.     int            initialized;
  65.     void            DoInitialize(void);
  66. public:
  67.     virtual int    read(void * buffer, int buflen);
  68.     virtual int write(void * buffer, int buflen);
  69.     virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  70.     virtual int    ioctl(unsigned int request, void *argp);
  71.     virtual int    isatty();
  72. };    
  73.  
  74. class SIOUXSocketDomain : public FileSocketDomain {
  75.     SIOUXSocket *    singleton;
  76. public:
  77.     SIOUXSocketDomain()    :    FileSocketDomain(AF_UNSPEC, true, false), singleton(nil)    {    }
  78.     
  79.     virtual Boolean Yours(const GUSIFileRef & ref, Request request);
  80.     virtual Socket * open(const GUSIFileRef & ref, int oflag);
  81. };
  82.  
  83. #if GENERATING68K
  84. #pragma segment SIOUX
  85. #endif
  86.  
  87. /************************ SIOUXSocket members ************************/
  88.  
  89. void SIOUXSocket::DoInitialize()
  90. {
  91.     if ( initialized ) return;
  92.     initialized++;
  93.     InstallConsole(0);
  94.     GUSISetEvents(GUSISIOUXEvents);
  95. }
  96.  
  97. SIOUXSocket::SIOUXSocket()
  98. {
  99.     initialized = 0;
  100.     if ( !GUSIConfig.DelayConsole() )
  101.         DoInitialize();
  102. }
  103.  
  104. SIOUXSocket::~SIOUXSocket()
  105. {
  106.     RemoveConsole();
  107. }
  108.  
  109. int SIOUXSocket::ioctl(unsigned int request, void *)
  110. {
  111.     if ( !initialized) DoInitialize();
  112.     switch (request)    {
  113.     case FIOINTERACTIVE:
  114.         return 0;
  115.     default:
  116.         return GUSI_error(EOPNOTSUPP);
  117.     }
  118. }
  119.  
  120. int SIOUXSocket::read(void * buffer, int buflen)
  121. {
  122.     if ( !initialized) DoInitialize();
  123.     fflush(stdout);
  124.     
  125.     return ReadCharsFromConsole((char *) buffer, buflen);
  126. }
  127.  
  128. int SIOUXSocket::write(void * buffer, int buflen)
  129. {
  130.     if ( !initialized) DoInitialize();
  131.     return WriteCharsToConsole((char *) buffer, buflen);
  132. }
  133.  
  134. static Boolean input_pending()
  135. {
  136.     QHdrPtr eventQueue = LMGetEventQueue();
  137.     EvQElPtr element = (EvQElPtr)eventQueue->qHead;
  138.     
  139.     // now, count the number of pending keyDown events.
  140.     while (element != nil) {
  141.         if (element->evtQWhat == keyDown || element->evtQWhat == autoKey)
  142.             return true;
  143.         element = (EvQElPtr)element->qLink;
  144.     }
  145.     
  146.     return false;
  147. }
  148.  
  149. int SIOUXSocket::select(Boolean * canRead, Boolean * canWrite, Boolean * exception)
  150. {
  151.     int        goodies     =     0;
  152.         
  153.     if ( !initialized) DoInitialize();
  154.     fflush(stdout);
  155.     
  156.     if (canRead) 
  157.         if (*canRead = input_pending())
  158.             ++goodies;
  159.     
  160.     if (canWrite) {
  161.         *canWrite = true;
  162.         ++goodies;
  163.     }
  164.     
  165.     if (exception)
  166.         *exception = false;
  167.     
  168.     return goodies;
  169. }
  170.  
  171. int SIOUXSocket::isatty()
  172. {
  173.     return 1;
  174. }
  175.  
  176. /********************* SIOUXSocketDomain members **********************/
  177.  
  178. #ifdef MSLGUSI
  179.     extern void GUSISetupMSLSIOUX();
  180. #endif
  181.  
  182. extern "C" void GUSIwithSIOUXSockets()
  183. {
  184.     static SIOUXSocketDomain    SIOUXSockets;
  185.     SIOUXSockets.DontStrip();
  186. #ifdef MSLGUSI
  187.     GUSISetupMSLSIOUX();
  188. #endif
  189. }
  190.  
  191. Boolean SIOUXSocketDomain::Yours(const GUSIFileRef & ref, FileSocketDomain::Request request)
  192. {
  193.     if (ref.spec || (request != willOpen && request != willStat))
  194.         return false;
  195.     
  196.     switch (ref.name[4] | 0x20) {
  197.     case 's':
  198.         if ((ref.name[5] | 0x20) != 't' || (ref.name[6] | 0x20) != 'd')
  199.             return false;
  200.         switch (ref.name[7] | 0x20) {
  201.         case 'i':
  202.             if ((ref.name[8] | 0x20) != 'n' || ref.name[9])
  203.                 return false;
  204.             return true;
  205.         case 'o':
  206.             if ((ref.name[8] | 0x20) != 'u' || (ref.name[9] | 0x20) != 't' || ref.name[10])
  207.                 return false;
  208.             return true;
  209.         case 'e':
  210.             if ((ref.name[8] | 0x20) != 'r' || (ref.name[9] | 0x20) != 'r' || ref.name[10])
  211.                 return false;
  212.             return true;
  213.         default:
  214.             return false;
  215.         }
  216.     case 'c':
  217.         if (    (ref.name[5] | 0x20) != 'o' || (ref.name[6] | 0x20) != 'n'
  218.             || (ref.name[7] | 0x20) != 's' || (ref.name[8] | 0x20) != 'o'
  219.             || (ref.name[9] | 0x20) != 'l' || (ref.name[10] | 0x20) != 'e')
  220.             return false;
  221.         switch (ref.name[11]) {
  222.         case 0:
  223.             return true;
  224.         default:
  225.             return false;
  226.         }
  227.     default:
  228.         return false;
  229.     }
  230. }
  231.  
  232. Socket * SIOUXSocketDomain::open(const GUSIFileRef &, int)
  233. {
  234.     if (!singleton)
  235.         singleton = new SIOUXSocket();
  236.     ++*singleton;
  237.     
  238.     return singleton;
  239. }
  240.